package com.tky.conference.implInterfaces;

import android.util.Log;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.tky.conference.base.UrlConstant;
import com.tky.conference.dao.AppointInfo;
import com.tky.conference.entity.FailRequest;
import com.tky.conference.entity.LoginOutInfo;
import com.tky.conference.interfaces.AddUpdateInter;
import com.tky.conference.interfaces.HttpResultListener;

import org.apache.http.Header;
import org.json.JSONObject;

/**
 * Created by cll on 2015/12/16.
 */
public class AddUpdateImpl implements AddUpdateInter {
    private String TAG = "LoginInfoImpl";
    private LoginOutInfo loginOutInfobj;
    private FailRequest failRequestobj;
    private HttpResultListener httpResultListener;

    public AddUpdateImpl(HttpResultListener httpResultListener) {
        this.httpResultListener = httpResultListener;
    }
    @Override
    public void addUpdate(String account, String hysid, String jlryids, String fzryids, String chryids,
                          String stime, String etime, String hydd, String hyzt, String hyms,
                          String zbbm, String zbgz, String id, String message) {
        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("account", account);
        params.put("hysid", hysid);
        params.put("jlryids", jlryids);
        params.put("fzryids", fzryids);
        params.put("chryids", chryids);
        params.put("stime", stime);
        params.put("etime", etime);
        params.put("hydd", hydd);
        params.put("hyzt", hyzt);
        params.put("hyms", hyms);
        params.put("zbbm", zbbm);
        params.put("zbgz", zbgz);
        params.put("id", id);
        params.put("message", message);
        client.setTimeout(UrlConstant.TIME_OUT);
        client.post(
                UrlConstant.ADDUPDATE,
                params,
                new AsyncHttpResponseHandler() {
                    @Override
                    public void onFailure(int arg0, Header[] arg1, byte[] arg2,
                                          Throwable arg3) {
                        System.out.println("fail");
                        try {
                            System.out.println("fail");
                            try {
                                failRequestobj = new FailRequest();
                                failRequestobj.setStatus("-1");
                                failRequestobj.setMsg("网络异常");
                                httpResultListener.onResult(failRequestobj);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
                        System.out.println("success");
                        Log.i(TAG, new String(arg2));
                        try {
                            JSONObject jsonObject=new JSONObject(new String(arg2));
                            loginOutInfobj=new LoginOutInfo();
                            loginOutInfobj.setStatus(jsonObject.getString("status"));
                            loginOutInfobj.setMsg(jsonObject.getString("msg"));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        httpResultListener.onResult(loginOutInfobj);
                    }
                    @Override
                    public void onStart() {
                        super.onStart();
                        httpResultListener.onStartRequest();
                    }

                    @Override
                    public void onFinish() {
                        super.onFinish();
                        httpResultListener.onFinish();
                    }
                }
        );
    }
}
